<template>
  <div>
    <el-dialog
      :visible="addDialog"
      width="50%"
      :before-close="closeDialog"
      top="5vh"
      :close-on-click-modal="false"
    >
      <div class="dialogTitle" slot="title">编辑员工信息</div>
      <el-form :model="form" label-width="180px">
        <el-form-item label="姓名:">
          <el-input
            v-model="form.name"
            autocomplete="off"
            placeholder="请输入"
          />
        </el-form-item>

        <el-form-item label="身份证号码:">
          <el-input
            v-model="form.identityCard"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
        <el-form-item label="所属公司:">
          <el-select v-model="form.enterpriseId" placeholder="请选择所属公司">
            <el-option
              v-for="item in options1"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="安全责任状:">
          <el-upload
            action=""
            :http-request="UploadImage3"
            :file-list.sync="fileList"
            :limit="1"
            list-type="picture-card"
            :before-upload="handleBeforeUpload"
            :on-exceed="handleExceed"
            :on-change="handleFileChange"
          >
            <i v-if="fileList.length < 1" slot="default" class="el-icon-plus" />
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="file.url"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete" />
                </span>
              </span>
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!form.id" class="addBtn" type="primary" @click="submit"
          >确定新增</el-button
        >
        <el-button v-else class="addBtn" type="primary" @click="submit"
          >确定修改</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addEmployee,
  updateEmployee,
  companyList,
  employeeDetail,
} from "@/api/employeeInfo";
import { Message } from "element-ui";
import { uploadApi } from "@/api/login";

export default {
  name: "addFire",
  props: {
    addDialog: {
      type: Boolean,
      default: false,
    },
    dialogId: {
      type: String,
      default: null,
    },
  },
  components: {},
  data() {
    return {
      form: {
        name: "",
        identityCard: "",
        enterpriseName: "",
      },
      options1: [],
      fileList: [],
      uploaderSubmit: [],
    };
  },
  created() {},
  methods: {
    async submit() {
      const findName = this.options1.find((item) => {
        return item.id == this.form.enterpriseId;
      });
      console.log(findName);
      if (this.form.id) {
        const res = await updateEmployee({
          id: this.form.id,
          name: this.form.name,
          identityCard: this.form.identityCard,
          enterpriseId: this.form.enterpriseId,
          enterpriseName: findName.name,
          safeResponsibilityStatementOne: this.uploaderSubmit[0].url,
          parkId: 1,
          parkName: "印染园区",
        });

        if (res.code == 0) {
          this.$message.success("修改成功");
          this.closeDialog();
          this.$parent.employeePage();
        } else {
          this.$message.error("新增失败");
        }
      } else {
        const res = await addEmployee({
          name: this.form.name,
          identityCard: this.form.identityCard,
          enterpriseId: this.form.enterpriseId,
          enterpriseName: findName.name,
          safeResponsibilityStatementOne: this.uploaderSubmit[0].url,
          parkId: 1,
          parkName: "印染园区",
        });

        if (res.code == 0) {
          this.$message.success("新增成功");
          this.closeDialog();
          this.$parent.employeePage();
        } else {
          this.$message.error("新增失败");
        }
      }
    },
    closeDialog() {
      this.form = {
        name: "",
        identityCard: "",
        enterpriseName: "",
      };
      this.fileList = [];
      this.uploaderSubmit = [];
      this.$emit("update:addDialog", false); // 触发父组件更新
    },

    async UploadImage3(file) {
      this.openLoading();
      const formData = new FormData();
      formData.append("file", file.file);

      const res = await uploadApi(formData);
      if (res.code === 0) {
        this.uploaderSubmit.push({
          url: res.data,
          name: file.file.name,
          uid: file.file.uid,
        });
      } else {
        this.fileList.pop();
      }
      this.openLoading().close();
    },
    handleBeforeUpload(file) {
      const suffix =
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/jpeg";
      const isLt1M = file.size / 1024 / 1024 < 1;
      if (!suffix) {
        Message.error("只能上传图片！");
      }
      if (!isLt1M) {
        Message.error("上传图片大小不能超过1MB!");
      }
    },
    handleExceed() {
      this.$message.warning("最多上传 1 个文件");
    },
    handleFileChange(file, fileList) {
      this.fileList = fileList; // 手动同步
    },
    handleRemove(e) {
      this.uploaderSubmit = this.uploaderSubmit.filter((item) => {
        return item.uid !== e.uid;
      });
      this.fileList = this.fileList.filter((item) => {
        return item.uid !== e.uid;
      });
    },
    async getCompanyList() {
      const res = await companyList();
      console.log(res);
      this.options1 = res.data;
    },
    async getEmployeeDetail(id) {
      const res = await employeeDetail({ id });
      this.form = res.data;
      this.fileList = [
        {
          url: res.data.safeResponsibilityStatementOne,
          name: res.data.safeResponsibilityStatementOne,
          uid: res.data.safeResponsibilityStatementOne,
        },
      ]
      this.uploaderSubmit = [
        {
          url: res.data.safeResponsibilityStatementOne,
          name: res.data.safeResponsibilityStatementOne,
          uid: res.data.safeResponsibilityStatementOne,
        },
      ]
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
};
</script>

<style scoped lang="scss">
.dialogTitle {
  position: relative;
  padding-left: 10px; /* 调整文本与颜色条之间的间距 */
  font-weight: bold;
}
.dialogTitle::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  border-radius: 2px;
  transform: translateY(-50%);
  width: 4px; /* 颜色条的宽度 */
  height: 14px; /* 颜色条的高度 */
  background-color: #0077ff; /* 颜色条的颜色 */
}
.el-select {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding-right: 50px !important;
}
::v-deep .dialog-footer {
  text-align: center;
  .addBtn {
    background: #0077ff;
    border-radius: 4px 4px 4px 4px;
    border: none;
    color: #fff;
  }
}

::v-deep .el-upload--picture-card {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
::v-deep .el-upload-list--picture-card .el-upload-list__item-actions {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
::v-deep .el-upload-list--picture-card .el-upload-list__item {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
</style>
